<?php

class ConsoleAction extends CheckAction {
	
	
	//测试
	public function ceshi() {

		$this->display('Console:__blank');
	}
	

	//读取SESSION
	public function xianshi() {
		//	unset($_SESSION["edu"]);
		//	unset($_SESSION["kafu"]);
		//	session(null);
	
		dayin($_SESSION);
	}
	
	
	public function _initialize(){
		$this->theme(C('CONSOLE_THEME'));
	}
	
	
	//登录
	public function login() {
		if($_POST){
			$username = trim($_POST['username']);
			$password = md5(trim($_POST['password']));
			$verify = trim($_POST['verify']);
			
			if(md5($verify)==session('verify')) {
				$db = D('Admin');
				$map['username'] = $username;
				$map['password'] = $password;
				$map['status']   = 1;
				$result = $db->where($map)->get();
				
				if(!empty($result)) {
					session('admin',$result);
					session('se',time()+C('SESSION_TIME_OUT'));
					
					$this->redirect('console/home');
				}else{
					session('notice',"帐号或密码错误");
					$this->redirect('console/login');
				}
			}else{
				session('notice',"验证码错误");
				$this->redirect('console/login');
			}
		}
		
		$this->display('Console:login');
	}
	
	//退出
	public function logout() {
		session(null);
		$this->redirect('console/login');
	}
	
	//首页
	public function home() {
		$this->redirect('console/project');
		$this->display('Console:home');
	}
	
	//修改密码
	public function changepwd() {
		
		if($_POST) {
			$pwd  = md5($_POST['pwd']);
			$newpwd = md5($_POST['newpwd']);
			$newpwd2 = md5($_POST['newpwd2']);
	
			$db = D('Admin');
			$map['username'] = session('admin.username');
			$map['password'] = $pwd;
			$result = $db->where($map)->get();
			
			if(!empty($result)) {
				if($newpwd == $newpwd2) {
					$data['password'] = $newpwd;
					$db->where($map)->save($data);
					session('notice',"修改成功");
					
					//修改成功更新SESSION
					$result['password'] = $newpwd;
					session('admin',$result);
				}else{
					session('notice',"两次新密码必须相同");
				}
			}else{
				session('notice',"原密码错误");
			}
			
			$this->redirect('console/changepwd');
		}
	
		
		$this->display('Console:changepwd');
	}
	
	//权限管理
	public function authority() {
		$this->need_auth('admin');
		
		$db = D('Admin');
		$map = array();
		$map['username'] = array('not in',array(session('admin.username'),'admin'),'OR');
		$result = $db->where($map)->getlist();
		$authority = authority();//所有权限
		
		//提交编辑
		if($_POST) {
			$draw = array();
			$draw['id'] = $_POST['id'];
			unset($_POST['id']);
			$shuju = $_POST;
			$db->where($draw)->mysave($shuju);
			session('notice','修改成功');
			$this->redirect('console/authority');
		}
		
		$this->assign('result',$result);
		$this->assign('authority',$authority);
		$this->display('Console:authority');
	}
	
	//删除管理员
	public function user_del() {
		$this->need_auth('admin');
		
		if($_GET['id']!='') {
			$db = D('Admin');
			$draw = array();
			$draw['id'] = $_GET['id'];
			$db->where($draw)->delete();
			session('notice','删除成功');
		}else{
			session('notice','删除失败');
		}
		
		$this->redirect('console/authority');
	}
	
	//新建管理员
	public function user_add() {
		$this->need_auth('admin');
		
		//提交编辑
		if($_POST) {
			$db = D('Admin');
			$data = array();
			$data['username'] = trim($_POST['username']);
			$data['password'] = md5(trim($_POST['password']));
			$db->add($data);
			session('notice','新建成功');
			$this->redirect('console/authority');
		}
		
		$this->display('Console:user_add');
	}
	
	//项目列表
	public function project() {
		$this->need_auth('reservation');
		
		$db = D('Project');
		$map = array();
		
		import('ORG.Util.Page');
		$count = $db->where($map)->count();
		$Page = new Page($count,20);
		$Page->setConfig('header','个');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','末页');
		$Page->setConfig('theme',"<li><a class='info'>共 %totalRow% %header% %nowPage%/%totalPage% 页</a></li> <li>%first%</li> <li>%upPage%</li> <li>%linkPage%</li> <li>%downPage%</li> <li>%end%</li>");
		
		$show = $Page->show();
		$result = $db->where($map)->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->getlist();
		
		$this->assign('page',$show);
		$this->assign('result',$result);
		$this->display('Console:project');
	}
	
	//删除项目
	public function project_del() {
		$this->need_auth('reservation');
		
		if($_GET['id']!='') {
			$db = D('Project');
			$draw = array();
			$draw['id'] = $_GET['id'];
			$db->where($draw)->delete();
			session('notice','删除成功');
		}else{
			session('notice','删除失败');
		}
	
		$this->redirect('console/project',array('p'=>$_GET['p']));
	}
	
	//修改项目
	public function project_edit() {
		$this->need_auth('reservation');
		
		$db = D('Project');
		$draw = array();
		$draw['id'] = $_GET['id'];
		$result = $db->where($draw)->get();
		
		if($_POST) {
			if($_FILES['cover']['name']!='') {
				import('ORG.Net.UploadFile');
				$upload = new UploadFile();// 实例化上传类
				$upload->maxSize  = 1048576;// 设置附件上传大小1M
				$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->thumb = true;
				$upload->thumbPrefix = '';
				$upload->thumbMaxWidth = '360';
				$upload->thumbMaxHeight = '450';
				$upload->savePath =  C('PIC_ROOT').'/project/';// 设置附件上传目录
				if(!$upload->upload()) {// 上传错误提示错误信息
					$notice = $upload->getErrorMsg();
					$notice = " - 其他提示信息：".$notice;
				}else{//上传成功 获取上传文件信息
					$info =  $upload->getUploadFileInfo();
					$data['cover'] = $info[0]['savename'];
					
					//删除原来的图片
					delfile('project',$result['cover']);
				}
			}
			
			$data['name'] = $_POST['name'];
			$data['dep'] = $_POST['dep'];
			$data['content'] = $_POST['content'];
			$data['star'] = $_POST['star'];
			$data['price'] = $_POST['price'];
			$data['num'] = $_POST['num'];
			$data['myorder'] = $_POST['myorder'];
			$data['buy'] = $_POST['buy'];
			$data['status'] = $_POST['status'];
			$db->where($draw)->save($data);
			session('notice','更新成功'.$notice);
			$this->redirect('console/project_edit',array('id'=>$_GET['id']));
		}
		
		$this->assign('result',$result);
		$this->display('Console:project_edit');
	}
	
	//新建项目
	public function project_add() {
		$this->need_auth('reservation');
		
		$db = D('Project');
		
		if($_POST) {
			if($_FILES) {
				import('ORG.Net.UploadFile');
				$upload = new UploadFile();// 实例化上传类
				$upload->maxSize  = 1048576;// 设置附件上传大小1M
				$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->thumb = true;
				$upload->thumbPrefix = '';
				$upload->thumbMaxWidth = '360';
				$upload->thumbMaxHeight = '450';
				$upload->savePath =  C('PIC_ROOT').'/project/';// 设置附件上传目录
				if(!$upload->upload()) {// 上传错误提示错误信息
					$notice = $upload->getErrorMsg();
					$notice = " - 其他提示信息：".$notice;
				}else{//上传成功 获取上传文件信息
					$info =  $upload->getUploadFileInfo();
					$data['cover'] = $info[0]['savename'];
				}
			}
				
			$data['name'] = $_POST['name'];
			$data['dep'] = $_POST['dep'];
			$data['content'] = $_POST['content'];
			$data['star'] = $_POST['star'];
			$data['price'] = $_POST['price'];
			$data['num'] = $_POST['num'];
			$data['myorder'] = $_POST['myorder'];
			$data['ctime'] = date('Y-m-d H:i:s');
			$data['buy'] = $_POST['buy'];
			$data['status'] = $_POST['status'];
			$id = $db->add($data);
			session('notice','新建成功'.$notice);
			$this->redirect('console/project_edit',array('id'=>$id));
		}
		
		$this->display('Console:project_add');
	}
	
	//订单列表
	public function reservation() {
		$this->need_auth('reservation');
	
		$db = D('Reservation');
		$map = array();
	
		import('ORG.Util.Page');
		$count = $db->where($map)->count();
		$Page = new Page($count,20);
		$Page->setConfig('header','个');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','末页');
		$Page->setConfig('theme',"<li><a class='info'>共 %totalRow% %header% %nowPage%/%totalPage% 页</a></li> <li>%first%</li> <li>%upPage%</li> <li>%linkPage%</li> <li>%downPage%</li> <li>%end%</li>");
	
		$show = $Page->show();
		$result = $db->where($map)->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->getlist();
		
		$this->assign('page',$show);
		$this->assign('result',$result);
		$this->display('Console:reservation');
	}
	
	
	//修改订单
	public function reservation_edit() {
		$this->need_auth('reservation');
	
		$db = D('Reservation');
		$draw = array();
		$draw['id'] = $_GET['id'];
		$result = $db->where($draw)->get();
		
		if($_POST) {
			$data['status'] = $_POST['status'];
			$data['pay_status'] = $_POST['pay_status'];
			$data['pay_time'] = $_POST['pay_time'];
			$data['pay_type'] = $_POST['pay_type'];
			
			$db->where($draw)->save($data);
			session('notice','更新成功'.$notice);
			$this->redirect('console/reservation_edit',array('id'=>$_GET['id']));
		}
		
		$this->assign('result',$result);
		$this->display('Console:reservation_edit');
	}
	
}














